package com.kgc.easyshopping.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.easyshopping.dto.BizDto;
import com.kgc.easyshopping.entity.Address;
import com.kgc.easyshopping.entity.Order;
import com.kgc.easyshopping.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class AddressController {
    @Resource
    private AddressService addressService;

    @PostMapping("address")
    public PageInfo<Address> selectPageInfo(@RequestParam Map<String, Object> params){
        Map<String, Object> map = new HashMap<>();

        int pageNum =  Integer.parseInt((String) params.get("pageNum"));
        int pageSize = Integer.parseInt((String) params.get("pageSize"));
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

//        String userid = (String) params.get("userid");
//        if(userid!=null && userid!=""){
//            params.put("userid",userid);
//        }
        map.put("userid",18);

        BizDto<PageInfo> bizDto = new BizDto<PageInfo>();
        bizDto.setData(addressService.selectPageInfo(map));
        return bizDto.getData();
    }

    @PostMapping("Setdefault")
    @ResponseBody
    public boolean Setdefault(String aid){
        Address address = addressService.selectByPrimaryKey(aid);
//        Address address = new Address();
//        address.setAid(aid);
        address.setIsdefault(1);
        BizDto<Boolean> bizDto = new BizDto<Boolean>();
        bizDto.setData(addressService.updateByPrimaryKeySelective(address));
        return bizDto.getData();
    }

    @PostMapping("AddressShow")
    @ResponseBody
    public Address selectById(String aid){
        Address address = addressService.selectByPrimaryKey(aid);
        BizDto<Address> bizDto = new BizDto<>();
        bizDto.setData(address);
        return bizDto.getData();
    }

    @PostMapping("InsertAddress")
    @ResponseBody
    public String InsertAddress(@RequestBody Map<String, Object> map) throws ParseException {
        Address address = new Address();
        String userid = "18";
        Date date = new Date();
        String site = (String) map.get("address");
        String email = (String) map.get("email");
        String name = (String) map.get("name");
        String particulars = (String) map.get("particulars");
        String mobile = (String) map.get("mobile");
        String sign = (String) map.get("sign");
        String optimum = (String) map.get("optimum");


        address.setParticulars(site);
        address.setUserid(userid);
        address.setAddress(particulars);
        address.setCreatetime(date);
        address.setEmail(email);
        address.setName(name);
        address.setMobile(mobile);
        address.setSign(sign);
        if(optimum!=null && optimum!=""){
            DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = fmt.parse(optimum);
            address.setOptimum(date1);
        }
        BizDto<String> bizDto = new BizDto<>();
        bizDto.setData(addressService.insert(address));
        return  bizDto.getData();
    }

    @PostMapping("UpdateAddress")
    @ResponseBody
    public boolean UpdateAddress(@RequestBody Map<String, Object> map) throws ParseException {
        Address address = new Address();
        String userid = "18";
        Date date = new Date();
        String site = (String) map.get("address");
        String email = (String) map.get("email");
        String name = (String) map.get("name");
        String particulars = (String) map.get("particulars");
        String mobile = (String) map.get("mobile");
        String sign = (String) map.get("sign");
        String optimum = (String) map.get("optimum");
        String aid = (String) map.get("aid");

        address.setParticulars(site);
        address.setUserid(userid);
        address.setAddress(particulars);
        address.setCreatetime(date);
        address.setEmail(email);
        address.setName(name);
        address.setMobile(mobile);
        address.setSign(sign);
        address.setAid(aid);
        if(optimum!=null && optimum!=""){
            DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = fmt.parse(optimum);
            address.setOptimum(date1);
        }
        BizDto<Boolean> bizDto = new BizDto<>();
        bizDto.setData(addressService.updateAddress(address));
        return  bizDto.getData();
    }

    @PostMapping("DeleteAddress")
    @ResponseBody
    public boolean DeleteAddress(String aid){
        BizDto<Boolean> bizDto = new BizDto<>();
        bizDto.setData(addressService.deleteByPrimaryKey(aid));
        return  bizDto.getData();
    }

}
